/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.entity.Inquery;
import com.wii.elpro.entity.SpdcfItem;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.SpdcfUpdateItemForm;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author User
 */
public class SpdcfItemSaveAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            {
        HttpSession sess = request.getSession(true);
//        ActionForsward af = null;
        SpdcfUpdateItemForm f = (SpdcfUpdateItemForm) form;
        User user = (User)sess.getAttribute("userSession");
        if(user==null){
            return mapping.findForward("gagalLogin");            
        } 
//        String priceHpp = request.getParameter("priceHpp");
//        String marginKeuangan = request.getParameter("marginKeuangan");
//        String note = request.getParameter("note");
        List list = new ArrayList();
//        String kode="";
        try{
            if(sess.getAttribute("listSpdcfItem")!=null){
                list = (List)sess.getAttribute("listSpdcfItem");
            }
            SpdcfItem si = (SpdcfItem)sess.getAttribute("item");
            for(int x=0;x<list.size();x++){
                SpdcfItem si1 = (SpdcfItem)list.get(x);
                if(si1.getProduk().getKode().equals(si.getProduk().getKode())){
//                    System.out.println("HERE--------------->");
//                    System.out.println(f.getMarginKeuangan());
//                    System.out.println(f.getNote());
//                    System.out.println(f.getPriceHpp());
                    si1.setPriceHpp(f.getPriceHpp());
                    si1.setMarginKeuangan(f.getMarginKeuangan());
                    si1.setNote(f.getNote());
//                    kode=si1.getProduk().getKode();
                    break;
                }
            }
            sess.setAttribute("listSpdcfItem", list);
            sess.removeAttribute("item");
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return mapping.findForward(SUCCESS);
        
    }
}